package com.lions.client.view.gui;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.lions.client.model.AudioOutput;
import com.lions.client.model.Model;
import com.lions.client.view.modulepanel.ModulePanel;
import com.lions.client.view.popup.MyPopupPanel;

/**
 * Menu bar for the main GUI.
 * @author Brad
 *
 */
public class MyMenuBar extends MenuBar {

	/**
	 * Constructor that initializes the menu bar and its click handlers.
	 * @param m
	 * @param moduleP
	 */
	public MyMenuBar(Model m, ModulePanel moduleP) {
		super();
		model = m;
		modulePanel = moduleP;
		buildCommands();
		initComponents();
	}
	
	/**
	 * Inits the GUI's components
	 */
	private void initComponents() {
		fileMenu = new MenuBar(true);
		fileMenu.addItem("Retrieve GPS Data", error);
		fileMenu.addItem("Clear All Modules", clearLocalData);
		fileMenu.addItem("Play a Song", awesomeSong);
		addItem("File", fileMenu);
	}
	
	/**
	 * Build the menu commands that will be fired by
	 * 
	 */
	private void buildCommands() {
		error = new Command() {
			public void execute() {
				Window.alert("NOT FUNCTIONAL");
			}
		};
		
		clearLocalData = new Command() {
			public void execute() {
				Button ok = new Button("OK");
				ok.setStyleName("buttonstyle");
				ok.addClickHandler(new ClickHandler(){
					@Override
					public void onClick(ClickEvent event) {
						model.getHTMLStorage().storageClear();
						modulePanel.clearAllPanels();
						new AudioOutput().playClearStorage();
						popup.hide();
					}
				});
				Button cancel = new Button("Cancel");
				cancel.setStyleName("buttonstyle");
				cancel.addClickHandler(new ClickHandler(){
					@Override
					public void onClick(ClickEvent event) {
						popup.hide();
					}
				});
				HorizontalPanel panel = new HorizontalPanel();
				panel.add(ok);
				panel.add(cancel);
				popup = new MyPopupPanel(panel, "Clear Memory", "Do You Really Want To Clear All Modules?\n This Cannot Be Undone!");
			}
		};
		
		awesomeSong = new Command() {
			@Override
			public void execute() {
				new AudioOutput().playWindowsSong();				
			}
		};
	}
	
	private MyPopupPanel popup;
	private Model model;
	private ModulePanel modulePanel;
	private MenuBar fileMenu;
	private Command error;
	private Command clearLocalData;
	private Command awesomeSong;
}
